package com.cy.beanutils.benchmark;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;

@RequestMapping("benchmark")
@RestController
public class BenchmarkController {
    @Autowired
    private CarMapper carMapper;

    @RequestMapping("runMapstruct")
    @ResponseBody
    public Long runMapstruct() {
        long start = System.currentTimeMillis();
        Car car = new Car();
        car.setNumberOfSeats(5);
        car.setMake("demo");
        car.setType(CarType.MINI_CAR);
        for (int i = 0; i < 1000000; i++) {
            CarDto carDto = carMapper.carToCarDto(car);
        }

        return System.currentTimeMillis() - start;
    }

    @RequestMapping("runSpring")
    @ResponseBody
    public Long runSpring() {
        long start = System.currentTimeMillis();
        Car car = new Car();
        car.setNumberOfSeats(5);
        car.setMake("demo");
        car.setType(CarType.MINI_CAR);
        for (int i = 0; i < 1000000; i++) {
            CarDto carDto = new CarDto();
            BeanUtils.copyProperties(car, carDto);
        }

        return System.currentTimeMillis() - start;
    }

    @RequestMapping("runApache")
    @ResponseBody
    public Long runApache() throws InvocationTargetException, IllegalAccessException {
        long start = System.currentTimeMillis();
        Car car = new Car();
        car.setNumberOfSeats(5);
        car.setMake("demo");
        car.setType(CarType.MINI_CAR);
        for (int i = 0; i < 1000000; i++) {
            CarDto carDto = new CarDto();
            org.apache.commons.beanutils.BeanUtils.copyProperties(carDto, car);
        }

        return System.currentTimeMillis() - start;
    }

    @RequestMapping("runApacheProperties")
    @ResponseBody
    public Long runApacheProperties()
            throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        long start = System.currentTimeMillis();
        Car car = new Car();
        car.setNumberOfSeats(5);
        car.setMake("demo");
        car.setType(CarType.MINI_CAR);
        for (int i = 0; i < 1000000; i++) {
            CarDto carDto = new CarDto();
            org.apache.commons.beanutils.PropertyUtils.copyProperties(carDto, car);
        }

        return System.currentTimeMillis() - start;
    }

    @RequestMapping("runCglib")
    @ResponseBody
    public Long runCglib() {
        long start = System.currentTimeMillis();
        Car car = new Car();
        car.setNumberOfSeats(5);
        car.setMake("demo");
        car.setType(CarType.MINI_CAR);
        BeanCopier beanCopier = BeanCopier.create(Car.class, CarDto.class, false);
        for (int i = 0; i < 1000000; i++) {
            CarDto carDto = new CarDto();
            beanCopier.copy(car, carDto, null);
        }

        return System.currentTimeMillis() - start;
    }
}
